<?php

/* * 
 * 分类模型
 */
namespace Admin\Model;
use Common\Model\CommonModel;



class ArticleModel extends CommonModel {

    //array(验证字段,验证规则,错误提示,[验证条件,附加规则,验证时间])
    protected $_validate = array(
        array('title', 'require', '标题不能为空！', 1, 'regex', 3),
    	array('model', 'require', '模型不能为空！', 1, 'regex', 3),    
    	array('content', 'require', '内容不能为空！', 1, 'regex', 3),
    );

  
    
    protected $_auto = array (       
    		array('uid',UID), 
    		array('updatetime','time',3,'function') ,    
    		array('inputtime','time',1,'function') , 
    		array('tags','trim',3,'function') ,
    	
     );
     
    public function add_data($v){
    	
       $data = $this->create($v, 1);
        if ($data) {
        	
        	$data['desc'] = $data['desc']?$data['desc']:$this->get_desc($data['content'], $v['add_desc_len']);
        	$data['thumb'] = $data['thumb']?$data['thumb']:$this->getThumb($data['content'], $v['add_thumb_no']);
        	
        	
            //添加数据
            $id = $this->add($data);
            if ($id) {
            	
            	$ss[id]=$id;
            	$ss[url]="/article-".$id;
            	$this->save($ss);
                return $id;
                
            } else {
                $this->error = '添加失败！';
                return false;
            }
        } else {
            return false;
        }
    	
    }
    
    
    public function edit_data($v){
    	 
    	$data = $this->create($v, 1);
    	if ($data) {
    		 
    		$data['desc'] = $data['desc']?$data['desc']:$this->get_desc($data['content'], $v['add_desc_len']);
    		$data['thumb'] = $data['thumb']?$data['thumb']:$this->getThumb($data['content'], $v['add_thumb_no']);
    		$data[url]="/article-".$v[id];
    		//添加数据
    		$id = $this->save($data);
    		if ($id) {
    			 
    			 
    			return $id;
    		} else {
    			$this->error = '编辑失败！';
    			return false;
    		}
    	} else {
    		return false;
    	}
    	 
    }
    
    /**
     * 栏目ur封装
     * @param type $data 页面提交数据
     * @return boolean]
     */
    
    public function get_cate_url($v){
    	
    	if($v[catdir]){
    		return "/".$v[catdir];
    	}else{
    		
    		return "/cate-".$v[id];
    	}
    	
    	
    	
    	
    }
    
    /**
     * 路由规则组装
     * @param type $data 页面提交数据
     * @return boolean]
     */
    
    public function get_route($v){
    	
    	
    		
    		if($v[catdir]){
    		   return array(
    		    	
    		    		'/^'.$v[catdir]."$/"=>"Home/Article/index?catid=".$v[id],
    		    );
    		    

    		   
    		}
    		
    	
    
    
    
    
    }
    	 
    	 
    

    /**
     * 自动获取简介
     * @param type $data
     */
    public function get_desc($content,$introcude_length) {
    
    
    	$desc= str_cut(str_replace(array("\r\n", "\t", '[page]', '[/page]', '&ldquo;', '&rdquo;', '&nbsp;'), '', strip_tags($content)), $introcude_length);
    	return $desc;
    
    
    }
    
    /**
     * 获取标题图片
     * @param type $data
     */
    protected function getThumb($content ,$auto_thumb_no) {
    
    	$auto_thumb_no =  intval($auto_thumb_no)-1;
    	if (preg_match_all("/(src)=([\"|']?)([^ \"'>]+\.(gif|jpg|jpeg|bmp|png))\\2/i", $content, $matches)) {
    		return $matches[3][$auto_thumb_no];
    	}
    
    }
    
    

	}

?>